var codeUuid;
var times=0;
$(function(){
    //localMsgToIndex();
    $("#btn").bind('click',function(){
        submitLogin();
    });

    $(".forget").bind('click',function(){
        window.location=server_url+'forgetPass.do';
    });

})
function refreshCaptcha() {
    codeUuid=uuid();
    $.ajax({
        type : "get",
        url : server_url+'rest/genCaptchaImage.do?codeUuid='+codeUuid,
        dataType:'json',
        jsonp:'callback',
        async : false,
        success : function(result) {
            if (result.retCode == '0000') {
                data=result.data;
                $('#captchaImage').attr('src',server_url+data);
            }
            else{
                data="";
            }
        }
    });
}
//回车
document.onkeydown = function (event){
    if (event.keyCode==13) //回车键的键值为13
        submitLogin();
};
function submitLogin() {
    var isSupply = params()["supply"];
    times+=1;
    var username = $('#dlNumber').val();
    var userpasswd = $('#dlPassword').val();
    var yzm = $('#yzm').val();
    var jsonObj =[];
    jsonObj.push({name:'username',value:$('#dlNumber').val()});
    jsonObj.push({name:'userpasswd',value:$('#userpasswd').val()});
    jsonObj.push({name:'yzm',value:$('#yzm').val()});
    jsonObj.push({name:'codeUuid',value:codeUuid});
    jsonObj.push({name:'times',value:times});
    if(username==''){
        easyAlert("请输入用户名");
        times-=1;
        return false;
    }
    if(userpasswd==''){
        times-=1;
        easyAlert("请输入密码");
        return false;
    }

    $.ajax({
        type : "post",
        url : server_url+'rest/staff/login.do?isSupply='+isSupply+'&username='+username+'&userpasswd='+userpasswd+'&codeUuid='+codeUuid+'&code='+yzm+'&times=1',
        dataType:'json',
        async : false,
        success : function(result) {
            if (result.retCode == '0000') {
                token=result.data.token;
                username=result.data.username;
                userid=result.data.userid;
                $("#token").attr('value',token);
                if(localStorage){
                    localStorage.setItem("token",token);
                    localStorage.setItem("username",username);
                    localStorage.setItem("userid",userid);
                    localStorage.setItem("staffId", userid);
                    localStorage.setItem("user",JSON.stringify(result.data));
                }
                window.location.href = server_url + 'staff/index.do';
            }
            else if(result.retCode == '1004')
            {
                easyAlert("验证码错误!");
                return false;
            }
            else if(result.retCode == '1005')
            {
                easyAlert("用户名或者密码错误");
                return false;
            }
            else if(result.retCode == '1002'){
                easyAlert("用户不存在!");
                return false;
            }
            else if(result.retCode == '1003'){
                easyAlert("用户名没有权限");
                return false;
            }
        },
        error: function(){
            easyAlert("服务器异常");
        }

    });
}

function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}

function getcodeUuid(){
    codeUuid=uuid();
    $.ajax({
        type : "get",
        url : server_url+'rest/genCaptchaImage.do?codeUuid='+codeUuid,
        dataType:'json',
        jsonp:'callback',
        async : false,
        success : function(result) {
            if (result.retCode == '0000') {
                data=result.data;
                $('#captchaImage').attr('src',server_url+data);
            }
            else{
                data="";
            }
        }
    });
}
function goForgetHtm(){
    window.location=server_url+"forgetPass.do?supply=1";
}